package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void insert(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        //关联关系
        if (ids != null && ids.length >0){
            for (Long rid : ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //关联关系,批量插入
        if (ids != null && ids.length >0){
            for (Long rid : ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public Employee get(Long id) {
       return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.queryForList(qo);
        return new PageInfo<>(employees);
        /*int totalCount = employeeMapper.queryForCount(qo);
        if (totalCount ==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                    Collections.emptyList(),totalCount);
        }

        //获取总页数
        int totalPage = totalCount % qo.getPageSize() ==0 ? totalCount / qo.getPageSize()
                : totalCount / qo.getPageSize() +1;
        //如果总页数 < 当前页
        if (qo.getCurrentPage() > totalPage){
            qo.setCurrentPage(totalPage);
        }

        List<Employee> employeeList = employeeMapper.queryForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                employeeList,totalCount);*/
    }

    @Override
    public Employee login(String username, String password) {

        if (!StringUtils.hasText(username)){
            throw new RuntimeException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)){
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.checkByUsernameAndPassword(username,password);
        if (employee == null){
            throw new RuntimeException("用户名和密码错误");
        }
        return employee;
    }

    @Override
    public void updatePwdByIdAndOldPwd(Long id,String oldPassword,String newPassword) {

        //获取当前登录用户
        Employee employee = UserContext.getCurrentUser();
        if (id == null){
            throw new RuntimeException("登录用户id为空");
        }
        if (!StringUtils.hasText(oldPassword)){
            throw new RuntimeException("原密码不能为空");
        }
        if (!StringUtils.hasText(newPassword)){
            throw new RuntimeException("新密码不能为空");
        }
        if (!employee.getPassword().equals(oldPassword)){
            throw new RuntimeException("原密码不正确");
        }
        employeeMapper.updatePwdByIdAndOldPwd(id,oldPassword,newPassword);
    }

    @Override
    public void resetPwdByAdmin(String newPassword, Long id) {
        if (id == null){
            throw new RuntimeException("用户id为空");
        }

        if (!StringUtils.hasText(newPassword)){
            throw new RuntimeException("密码不能为空");
        }
        employeeMapper.resetPwdByAdmin(newPassword,id);
    }
}
